<?php 
/**
* 上传类
*/
class Upload 
{
	public $path = "./upload/";
	public $type = array('jpg');
	public $max_size = "5000000";
	public $data;
	public $error;
	public $info;
	public function __construct($config)
	{
		foreach ($config as $k => $v) {
			$this->$k = $v;
		}
		$this->data = current($_FILES);
	}

	public function save()
	{
		$this->_checkDir();

		if(!$this->_checkType()){
			return false;
		}
		if(!$this->_checkMax()){
			return false;
		}

		if(!$this->_moveFile()){
			return false;
		}else{
			return $this->info;
		}
	}

	private function _checkDir()
	{
		is_dir($this->path) || mkdir($this->path);
	}

	private function _checkType()
	{
		foreach ($this->data['name'] as $v) {
			$arr = explode('.',$v);
			//获取数组最后一个
			$str = end($arr);

			if(!in_array($str, $this->type)){
				$this->error = "上传后缀不允许";
				return false;
			}
		}
		return true;
	}

	private function _checkMax()
	{
		foreach ($this->data['size'] as $v) {

			if($v>$this->max_size){
				$this->error = "上传大小不允许";
				return false;
			}
		}
		return true;
	}

	private function _moveFile()
	{
		foreach ($this->data['name'] as $k => $v) {
			//获取文件的后缀
			$arr = explode('.',$v);
			//获取数组最后一个
			$str = end($arr);
			//生成一个唯一的ID
			$name = uniqid();
			//组文件名
			$filename =$name.".".$str;
			//移动临时文件
			$res = move_uploaded_file($this->data['tmp_name'][$k], $this->path.$filename);

			if($res){
				$this->info[]= $this->path.$filename;
			}

		}
		return true;
	}
	
}

 ?>